package com.budwk.app.sqfwV2.generator.weiqingmen;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.RectifyDTO;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 * <p>
 * 威清门消防安全排查 SafetyCheck
 * 上一次餐饮经营服务场所消防安全巡查台账.docx
 */
public class CateringServiceLastPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        Map<String, Object> customerParamMap = pdfDTO.getCustomerParamMap();
        Sqfw_formSummary_new lastFormSummary = (Sqfw_formSummary_new) customerParamMap.get("lastFormSummary");
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        boolean createFlag = false;
        if (lastFormSummary != null && fieldDTO.getAttributesOfInspectionUnits().equals("餐饮店面")) {
            SafetyCheckDTO lastFieldDTO = JSONObject.parseObject(lastFormSummary.getParamData(), SafetyCheckDTO.class);
            // 只有存在限期整改问题才需要显示上一次
            String hidden = BaseGenerator.getHiddenNotOther(lastFieldDTO, "限期整改");
            if ("无".equals(hidden)) return false;
            createFlag = true;
        }
        return createFlag;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/CateringService.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {

        Sqfw_formSummary_new lastFormSummary = (Sqfw_formSummary_new) pdfGenerateParam.getCustomerParamMap().get("lastFormSummary");
        SafetyCheckDTO fieldDTO = JSONObject.parseObject(lastFormSummary.getParamData(), SafetyCheckDTO.class);
        Map<String, Object> map = BeanUtil.beanToMap(lastFormSummary);

        paramMap.putAll(map);

        List<RectifyDTO> arrayOfFireSafetyPatrolRecords = fieldDTO.getArrayOfFireSafetyPatrolRecords();

        // 设置巡查内容状态
        BaseGenerator.setArrayOfFireSafetyPatrolRecords(paramMap, arrayOfFireSafetyPatrolRecords);

        // 巡查时间
        Date inspectionDate = fieldDTO.getInspectionDate();
        paramMap.put("yyyy", inspectionDate == null ? "" : DateUtil.year(inspectionDate));
        paramMap.put("mm", inspectionDate == null ? "" : DateUtil.month(inspectionDate) + 1);//月份从0开始,所以要加1
        paramMap.put("dd", inspectionDate == null ? "" : DateUtil.dayOfMonth(inspectionDate));

        // 现场整改
        List<String> sceneChange = BaseGenerator.getSceneChangeNum(arrayOfFireSafetyPatrolRecords);
        paramMap.put("sceneChange", StringUtils.join(sceneChange, "、"));
        // 限期整改
        paramMap.put("deadlineChange", StringUtils.join(BaseGenerator.getDeadlineChangeNum(arrayOfFireSafetyPatrolRecords), "、"));

        // 限期整改时间
        Date deadlineForRectification = fieldDTO.getDeadlineForRectification();
        paramMap.put("cmm", deadlineForRectification == null ? "" : DateUtil.month(deadlineForRectification) + 1);//月份从0开始,所以要加1
        paramMap.put("cdd", deadlineForRectification == null ? "" : DateUtil.dayOfMonth(deadlineForRectification));

        // 创建人的信息
        Object lastSignatureUrl = pdfGenerateParam.getCustomerParamMap().get("lastSignatureUrl");
        if (lastSignatureUrl != null) {
            paramMap.put(ClassUtil.getField(CommonParam::getCreateUserName), "");
        } else {
            paramMap.put(ClassUtil.getField(CommonParam::getCreateUserName),
                    pdfGenerateParam.getCustomerParamMap().get(ClassUtil.getField(CommonParam::getCreateUserName)));
        }

        // 复查情况
        paramMap.put("omm", "");
        paramMap.put("odd", "");
        paramMap.put("change", "");
        paramMap.put("notChange", "");
        paramMap.put("oUserName", "");
    }

    /**
     * 签名、盖章
     * @param xwpfTemplate         XWPDF文件
     * @param pdfGenerateParam 生成pdf的参数
     */
    protected void createXWPDFTemplateAfter(XWPFTemplate xwpfTemplate, PdfGenerateParam pdfGenerateParam) {
        Sqfw_formSummary_new lastFormSummary = (Sqfw_formSummary_new) pdfGenerateParam.getCustomerParamMap().get("lastFormSummary");
        SafetyCheckDTO fieldDTO = JSONObject.parseObject(lastFormSummary.getParamData(), SafetyCheckDTO.class);
        Object lastSignatureUrl = pdfGenerateParam.getCustomerParamMap().get("lastSignatureUrl");

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.networkImageSeal(xwpfDocument, "被巡查单位人员签名", fieldDTO.getCheckedUnitLeader(),
                    140, -10, 65);
            PictureUtils.networkImageSeal(xwpfDocument, "巡查人员签名", lastSignatureUrl == null ? "" : lastSignatureUrl.toString(),
                    95, -10, 65);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
